//
//  RecordBaseVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/9/24.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit
import SCLAlertView

class RecordBaseVC: UIViewController {
    var fromDate: Date!
    var toDate: Date!
    private var alertTopButton: SCLButton!
    private var alertBottomButton: SCLButton!
    var alertDoneButton: SCLButton!
    var dateAlert = SCLAlertView()
    
    var materialId: Int? //是否查看单个物料的记录
    
    func showDatePicker() {
        let appearance = SCLAlertView.SCLAppearance(showCloseButton: false, shouldAutoDismiss: false, hideWhenBackgroundViewIsTapped: true)
        dateAlert = SCLAlertView(appearance: appearance)
        alertTopButton = dateAlert.addButton("起始日期", target: self, selector: #selector(topBtTapped(_:)))
        alertBottomButton = dateAlert.addButton("结束日期", target: self, selector: #selector(alertBottomBtTapped(_:)))
        alertDoneButton = dateAlert.addButton("确定", target: self, selector: #selector(alertDoneBtTapped(_:)))
        dateAlert.showInfo("自定义日期", subTitle: "请选择起始日期与结束日期")
        
    }
    @objc func alertDoneBtTapped(_ button: UIButton!) {
        
    }
    @objc private func topBtTapped(_ button: SCLButton!) {
        DatePickerDialog().show("起始日期", doneButtonTitle: "完成", cancelButtonTitle: "关闭", defaultDate: Date(), minimumDate: Date(timeIntervalSince1970: 0), maximumDate: Date(timeIntervalSinceNow: 0), datePickerMode: .date) { (date) in
            if let dt = date {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd"
                self.fromDate = dt
                self.alertTopButton.setTitle("\(formatter.string(from: dt))", for: .normal)
            }
        }
    }
    @objc private func alertBottomBtTapped(_ button: SCLButton!) {
        guard fromDate != nil else {
            return
        }
        DatePickerDialog().show("结束日期", doneButtonTitle: "完成", cancelButtonTitle: "关闭", defaultDate: Date(), minimumDate: fromDate, maximumDate: Date(timeIntervalSinceNow: 0), datePickerMode: .date) { (date) in
            if let dt = date {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd"
                self.alertBottomButton.setTitle("\(formatter.string(from: dt))", for: .normal)
                self.toDate = dt
            }
        }
    }
}
